<template>
  <div class="app-container">
    <el-input v-model="search.keyword" placeholder="keyword" style="width:300px" />
    <el-button type="primary" @click="getList">Search</el-button>
    <el-button type="primary" @click="handleAddContact">Add </el-button>

    <el-table :data="contcatList" style="width: 100%;margin-top:30px;" border :empty-text="empty_text">
      <el-table-column align="center" label="Name" width="150">
        <template slot-scope="scope">
          {{ scope.row.contact_name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Client Name">
        <template slot-scope="scope">
          {{ scope.row.hotel_name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Contact No." width="150">
        <template slot-scope="scope">
          {{ scope.row.contact_tel }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Email">
        <template slot-scope="scope">
          {{ scope.row.contact_email }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Login" width="80">
        <template slot-scope="scope">{{ scope.row.login? 'Yes' : 'No' }}</template>
      </el-table-column>
      <el-table-column align="center" label="Notiﬁcation" width="100">
        <template slot-scope="scope">{{ scope.row.notify_open? 'Yes' : 'No' }}</template>
      </el-table-column>
      <el-table-column align="center" label="Email" width="100">
        <template slot-scope="scope">{{ scope.row.email_open? 'Yes' : 'No' }}</template>
      </el-table-column>
      <el-table-column align="center" label="Action" width="80">
        <template slot-scope="scope">
          <el-col :span="12">
            <el-link style="font-size:18px;" class="el-icon-edit icon-class" @click="handleEdit(scope)" />
          </el-col>
          <el-col :span="12">
            <el-link style="font-size:18px;" class="el-icon-delete icon-class" @click="handleDelete(scope)" />
          </el-col>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="search.page"
      :page-size="pagesize"
      style="margin-top:10px; float:right"
      @current-change="getPageList"
    />

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? 'Edit Contact Person' : 'New Contact Person'"
    >
      <el-form ref="connectForm" :model="hotelContact" :rules="rules" label-width="160px" label-position="left">
        <el-form-item label="Client & Hotel" prop="hotel_id">
          <el-select v-model="hotelContact.hotel_id" placeholder="Please Choose" no-data-text="Can't find record">
            <el-option
              v-for="item in hotelList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Name" prop="contact_name">
          <el-input v-model="hotelContact.contact_name" placeholder="Name" />
        </el-form-item>
        <el-form-item label="Contact No." prop="contact_tel">
          <el-input v-model="hotelContact.contact_tel" placeholder="Contact No." />
        </el-form-item>
        <el-form-item label="Email" prop="contact_email">
          <el-input v-model="hotelContact.contact_email" placeholder="Email" />
        </el-form-item>
        <el-form-item label="Login Web&APP" prop="isLogin">
          <el-checkbox v-model="hotelContact.login" />
        </el-form-item>
        <el-form-item v-if="hotelContact.login" label="Login Name" :prop="hotelContact.login?'user_login':''">
          <el-input v-model="hotelContact.user_login" placeholder="Login Name" />
        </el-form-item>
        <el-form-item v-if="hotelContact.login" label="Password" :prop="hotelContact.login&&dialogType != 'edit'?'user_pass':''">
          <el-input v-model="hotelContact.user_pass" type="password" placeholder="Password" />
        </el-form-item>
        <el-form-item label="Open Notiﬁcation" prop="notify_open">
          <el-checkbox v-model="hotelContact.notify_open" />
        </el-form-item>
        <el-form-item label="Open Email" prop="email_open">
          <el-checkbox v-model="hotelContact.email_open" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="confirmContact">Confirm</el-button>
      </div>
    </el-dialog>
    <!--
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? 'Edit Client Person' : 'New Client Person'"
    >
      <el-form :model="contcatInfo" label-width="150px" label-position="left">
        <el-form-item label="Name">
          <el-input v-model="contcatInfo.contact_name" placeholder="Name" />
        </el-form-item>
        <el-form-item label="Client Name">
          <el-input v-model="contcatInfo.hotel_name" placeholder="Client Name" />
        </el-form-item>
        <el-form-item label="Contact No">
          <el-input v-model="contcatInfo.contact_tel" placeholder="Contact No" />
        </el-form-item>
        <el-form-item label="Email">
          <el-input v-model="contcatInfo.contact_email" placeholder="Email" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button
          type="danger"
          @click="dialogVisible = false"
        >Cancel</el-button>
        <el-button type="primary" @click="confirmContact">Confirm</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { getContactList, deleteContact } from '@/api/contcat'
import { saveHotelContact } from '@/api/hotel/detail'
import { getDictionary } from '@/api/user'

// const defaultContcat = {
//   id: 0,
//   user_nickname: '',
//   hotel_name: '',
//   contact_no: '',
//   user_email: ''
// }

const defaultHotelContact = {
  id: 0,
  hotel_id: '',
  contact_name: '',
  contact_tel: '',
  contact_email: '',
  login: false,
  notify_open: false,
  email_open: false,
  user_login: '',
  user_pass: ''
}

export default {
  data() {
    return {
      hotelContact: Object.assign({}, defaultHotelContact),
      hotelList: [],
      dialogVisible: false,
      dialogType: 'new',
      empty_text: '',
      rules: {
        hotel_id: [
          { required: true, message: 'Please Choose Client', trigger: 'change' }
        ],
        user_login: [
          { required: true, message: 'please enter user name', trigger: 'blur' }
        ],
        user_pass: [
          { required: true, message: 'Please enter the password', trigger: 'blur' }
        ],
        contact_name: [
          { required: true, message: 'Please enter a contact', trigger: 'blur' }
        ],
        contact_tel: [
          { required: true, message: 'Please enter contact information', trigger: 'blur' }
        ],
        contact_email: [
          { required: true, message: 'Please input your email', trigger: 'blur' },
          { type: 'email', message: 'Please enter the correct email format', trigger: 'blur' }
        ]
      },
      search: {
        keyword: '',
        page: 1
      },
      total: 10,
      pagesize: 1,
      contcatList: [],
      contcatInfo: []
    }
  },
  computed: {
  },
  watch: {
    dialogVisible(newValue, oldValue) {
      // 清空验证
      if (this.$refs['connectForm']) this.$refs['connectForm'].clearValidate()
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    this.getList()
    this.getSelectList()
  },
  methods: {
    async getSelectList() {
      // 宾馆
      await getDictionary({ dict_type: 'hotel' }).then(result => {
        this.hotelList = result
      })
    },
    async getList() {
      this.empty_text = 'Loading ......'
      await getContactList(this.search).then(res => {
        this.contcatList = res.data
        this.total = res.total
        this.search.page = res.current_page
        this.pagesize = res.per_page
        this.empty_text = 'Can\'t find record in table'
      }).catch(() => {
        this.rolesList = []
        this.empty_text = 'Can\'t find record in table'
      })
    },
    async getPageList(value) {
      this.search.page = value
      this.empty_text = 'Loading ......'
      await getContactList(this.search).then(res => {
        this.contcatList = res.data
        this.total = res.total
        this.search.page = res.current_page
        this.pagesize = res.per_page
        this.empty_text = 'Can\'t find record in table'
      }).catch(() => {
        this.rolesList = []
        this.empty_text = 'Can\'t find record in table'
      })
    },
    handleAddContact() {
      this.hotelContact = Object.assign({}, defaultHotelContact)
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedNodes([])
      }
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.hotelContact = Object.assign({}, defaultHotelContact)
      this.dialogType = 'edit'
      this.dialogVisible = true
      // this.checkStrictly = true
      this.hotelContact.hotel_id = scope.row.hotel_id
      this.hotelContact.id = scope.row.id
      this.hotelContact.contact_name = scope.row.contact_name
      if (scope.row.login) {
        this.hotelContact.user_login = scope.row.user_login
      }
      this.hotelContact.contact_tel = scope.row.contact_tel
      this.hotelContact.contact_email = scope.row.contact_email
      this.hotelContact.login = scope.row.login === 1
      this.hotelContact.notify_open = scope.row.notify_open === 1
      this.hotelContact.email_open = scope.row.email_open === 1
      // this.$nextTick(() => {
      //   this.$refs.tree.setCheckedKeys(this.contcatInfo.routes)
      //   // set checked state of a node not affects its father and child nodes
      //   this.checkStrictly = false
      // })
    },
    handleDelete({ $index, row }) {
      this.$confirm('Confirm to remove the Contcat?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      })
        .then(async() => {
          await deleteContact({ hotel_contact_id: row.id })
          this.contcatList.splice($index, 1)
          this.$message({
            type: 'success',
            message: 'Delete succed!'
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    async confirmContact() {
      this.$refs.connectForm.validate(async valid => {
        if (valid) {
          await saveHotelContact(this.hotelContact).then(async() => {
            const { hotel_id, contact_name } = this.hotelContact
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              dangerouslyUseHTMLString: true,
              message: `
                  <div>hotel_id: ${hotel_id}</div>
                  <div>contact_name: ${contact_name}</div>
                `,
              type: 'success'
            })

            // 更新列表
            await this.getList()
          })
        }
      })
      // await saveContact(this.contcatInfo)

      // this.dialogVisible = false
      // this.$notify({
      //   title: 'Success',
      //   dangerouslyUseHTMLString: true,
      //   message: `
      //       <div>Success</div>
      //     `,
      //   type: 'success'
      // })

      // // 更新列表
      // this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
